<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ajax进行登录</title>
</head>

<body>
    用户名：<input type="text" id="ywm">
    密  码：<input type="password" id="paw">
    <button id="btn">确认</button>
</body>
<script>

    // ​	使用ajax进行登录：

    // ​		2个输入框 一个登录按钮

    // ​		提交字段有 username , password

    // ​		接口地址： https://liu.zzgoodqc.cn/login  POST方法请求

    window.onload = function () {
        btn.onclick = function () {

        var username = document.getElementById('ywm').value,
            password = document.getElementById('paw').value
        var xhttp = new XMLHttpRequest()
        xhttp.open('POST', 'https://liu.zzgoodqc.cn/login', true)
        xhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded')

        document.ondurationchange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                var dataobj = JSON.parse(xhttp.responseText)
                if (dataobj.code == 200) {
                    alert(dataobj.msg)
                }else{
                    alert(dataobj.msg)
                }
            }
        }
        var data = 'username=' + username + '&password=' + password
        xhttp.send(data)
    }
    }




</script>

</html>